package offer;

public class offer_0033 {
    public boolean verifyPostorder(int[] postorder) {
        int len=postorder.length;
        if(len==0) return true;

        boolean[] flag=new boolean[1];
        flag[0]=true;
        search(postorder,0,len-1,flag);
        return flag[0];
    }
    public void search(int[] postorder,int left,int right,boolean[] flag){
        if(right-left<=1) return;
        int i;
        int min=postorder[right];
        for( i=right-1;i>=left;--i){
            min=Math.min(min,postorder[i]);
            if(min<postorder[right]){
                break;
            }
        }
        for(int j=i;j>=left;--j){
            if(postorder[j]>postorder[right]) {flag[0]=false;return;}
        }
        if(flag[0]){
            search(postorder,left,i,flag);
            search(postorder,i+1,right-1,flag);
        }
    }
}
